<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 模型数据管理控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class BuyticketsController extends ThinkController{

  public function ticketslists(){
    $data = M('ticket')
    ->field('ticket.*,exhibition.name as ex_name')
    ->where(array('status'=>1))
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->select();
    foreach ($data as $key => $value) {
      # code...
      $data[$key]['begin_date'] = date('Y-m-d',$value['begin_date']);
      $data[$key]['end_date'] = date('Y-m-d',$value['end_date']);
    }
    $this->assign('list_data', $data);
    $this->display('Buytickets/buytickets');
  }

  public function ticketinfo(){
    $ticketId = I('get.ticketId');
    $data = M('ticket')
    ->field('ticket.*,exhibition.name as ex_name')
    ->where(array('status'=>1,'ticket.id'=>$ticketId))
    ->join('exhibition on exhibition.id = ticket.exhibition_id','left')
    ->find();
    $this->success($data);
  }

  public function buy(){
    $post = $_POST;
    $ticket_id = $post['ticket_id'];
    if(!$ticket_id){
      $this->error('缺少票务号');
    }
    $amount = $post['amount'];
    if(!$amount||$amount<=0){
      $this->error('请至少购买一张票！');
    }
    if(!$post['payment']){
      $this->error('请选择支付方式！');
    }

    // 生成购票订单
    // 查询ticket信息
    $re_ticket = M('ticket')->find($ticket_id);
    //票券错误信息处理
    $current_date = time();
    if($current_date<$re_ticket['begin_date']){
      $this->error('很抱歉，您选择的票券还未开售！');
    }
    if($current_date>($re_ticket['end_date']+3600*24)){
      $this->error('很抱歉，您选择的票券已停售！');
    }
    if($re_ticket['stock']<=0){
      $this->error('很抱歉，您选择的票券已售罄！');
    }

    $post['create_time'] = time();
    $post['site'] = 2;
    $post['status'] = 1;
    $post['price'] = $re_ticket['price'];
    $post['activity_price'] = $re_ticket['price_activity'];

    // 生成ticket_order订单
    $re_order = M('ticket_order')->add($post);
    $date = date("Ymd");
    // 更新订单号
    M('ticket_order')->where(array('id'=>$re_order))->save(array('order_num'=>$date.$re_order));

    //生成兑换码
    for($i=0;$i<$amount;$i++){
      $data['ticket_order_id'] = $re_order;
      $data['create_time'] = time();
      $data['redeem_time'] = $data['create_time'];
      $data['status'] = 1;

      $re_redeem = M('ticket_redeem_code')->add($data);
      $len = strlen($re_redeem);
      $id = substr($re_redeem,($len-2));
      M('ticket_redeem_code')->where(array('id'=>$re_redeem))->save(array('redeem_code'=>$data['create_time'].$id));
    }
    //减少票务库存
    M('ticket')->where(array('id'=>$ticket_id))->save(array('stock'=>$re_ticket['stock']-$amount));

    $this->success('购买成功！');
  }  

}